package zfl.fanglong.cn.service;

import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.Session;

import org.springframework.jms.support.converter.MessageConversionException;
import org.springframework.jms.support.converter.MessageConverter;

import zfl.fanglong.cn.domain.MyMessage;

public class MyMessageConverter implements MessageConverter{

	public Object fromMessage(Message arg0) throws JMSException,
			MessageConversionException {
		// TODO Auto-generated method stub
		if(!(arg0 instanceof MapMessage)){
			throw new MessageConversionException("Message isn't a map message!");
		}
		MapMessage mapMes = (MapMessage)arg0;
		MyMessage mm = new MyMessage();
		mm.setInfo(mapMes.getString("INFO"));
		mm.setText(mapMes.getString("TEXT"));
		
		return mm;
	}

	public Message toMessage(Object arg0, Session arg1) throws JMSException,
			MessageConversionException {
		// TODO Auto-generated method stub
		if(!(arg0 instanceof MyMessage)){
			throw new MessageConversionException("Object isn't a MyMessage!");
		}
		MyMessage mm = (MyMessage)arg0;
		MapMessage mme = arg1.createMapMessage();
		mme.setString("INFO",mm.getInfo());
		mme.setString("TEXT", mm.getText());
		return mme;
	}

}
